﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GenerateScripts.aspx.cs" Inherits="GPSTrackingWebsiteManager.Pages.GenerateScripts" %>

<!DOCTYPE html>
<link href="../../Css/GPSTrackingWebsite.css" rel="stylesheet" />
<link href="../../Css/messi.css" rel="stylesheet" />
<script src="../../Javascript/jquery-1.11.1.min.js"></script>
<script src="../../Javascript/messi.js"></script>
<script src="../../Javascript/Custom.js"></script>
<script src="../../Javascript/WindowMessageManager.js"></script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Genera Scrips</title>
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            
            var objWindowsMessageManager = new WindowsMessageManager();

            var opener = GetRadWindow();

            function GetRadWindow() {
                var oWindow = null;
                if (window.radWindow)
                    oWindow = window.radWindow;
                else if (window.frameElement.radWindow)
                    oWindow = window.frameElement.radWindow;
                return oWindow.BrowserWindow;
            }
            
            function initializeObjects() {
                opener.objMapControl.initializeForScripts();
            }

            function Markers() {
                var marker;
                var distance;
                var moreSecond;

                this.setMarker = function (marker) {
                    this.marker = marker;
                    this.distance = 0;
                    this.moreSecond = 0;
                };
            }

            

            function generateScript() {
                var txt = "Declare @Fecha datetime = 'YYYY/MM/DD HH:MM:SS'  \r\n";
                txt += "Declare @GPSImei nvarchar(30) = ''  \r\n";
                var distance = 0;
                for (i = 0; i < opener.globalGenerateScripts.markerList.length; i++) {

                    distance += opener.globalGenerateScripts.markerList[i].distance;
                    txt += "Insert Into TrackData(GPSImei, DateTime, Latitude, Longitude, Heading, Speed) values(@GPSImei, " + "DATEADD (ss, " + distance + " , @Fecha), " + opener.globalGenerateScripts.markerList[i].marker.position.lat() + ", " + opener.globalGenerateScripts.markerList[i].marker.position.lng() + ", 0, 0); \r\n";

                    if (opener.globalGenerateScripts.markerList[i].moreSecond > 0) {
                        for (ii = 0; ii < opener.globalGenerateScripts.markerList[i].moreSecond; ii++) {
                            distance += (5 * 60);
                            txt += "Insert Into TrackData(GPSImei, DateTime, Latitude, Longitude, Heading, Speed) values(@GPSImei, " + "DATEADD (ss, " + distance + " , @Fecha), " + opener.globalGenerateScripts.markerList[i].marker.position.lat() + ", " + opener.globalGenerateScripts.markerList[i].marker.position.lng() + ", 0, 0); \r\n";
                        }
                    }
                }

                setInfo(txt);

            }

            function setInfo(txt) {
                var input = document.getElementById('url');
                input.value = txt;
                input.focus();
                input.select();
            }

            function moreTime() {
                if (opener.globalGenerateScripts.index > -1) {
                    opener.globalGenerateScripts.markerList[opener.globalGenerateScripts.index].moreSecond += 1;
                }
            }

            function enableDisableEvent(checked) {
                enableDisableEventClickMap(checked);
            }

            this.enableDisableEventClickMap = function (checked) {
                if (checked) {
                    opener.objMapControl.clearListenerClic();
                    opener.objMapControl.addListenerclientMapForScripts();
                }
                else {
                    opener.objMapControl.clearListenerClic();
                }
            }
        </script>

    </telerik:RadCodeBlock>
</head>
<body onload="initializeObjects()">
    <form id="form1" runat="server">
        <input type="button" onclick="enableDisableEvent(true);" value="Habilitar Marcador" />
        <input type="button" onclick="enableDisableEvent(false);" value="Deshabilitar Marcador" />
        <input type="button" onclick="generateScript();" value="Generar Scripts" />
	    <input type="button" onclick="moreTime();" value="Aumentar 5 minutos al último marcador" />
	    <br/>
	    <textarea id="url" rows="5" cols="150"></textarea>

    </form>
</body>
</html>
